/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.news;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import bw.util.*;

public class NewsServlet extends HttpServlet
{
    public static final String MOVE_CATEGORY = "moveCat";
    public static final String DELETE_CATEGORY = "deleteCat";
    public static final String CREATE_CATEGORY = "createCat";
    public static final String TOGGLE_STAR = "toggleStar";
    public static final String STAR = "star";

    public static final String ACTION = "action";
    public static final String CATEGORY_ID = "categoryId";
    public static final String POSITION = "pos";
    public static final String NAME = "name";
    public static final String NEWS_ITEM_ID = "newsItemId";
    public static final String NEWS_SOURCE_ID = "newsSourceId";

    public void doGet ( HttpServletRequest req, HttpServletResponse res ) 
        throws ServletException, IOException
    {
        String action = req.getParameter( ACTION );

        res.setContentType( "text/html" );

        PrintWriter out = res.getWriter();

        NewsManager manager = NewsManager.getInstance();

        String catIdStr = null;
        NewsCategory cat = null;
        if ( ( catIdStr = req.getParameter( CATEGORY_ID ) ) != null ) 
        {
            int categoryId = Integer.parseInt( req.getParameter( CATEGORY_ID ) );
            cat = manager.getNewsCategoryForId( categoryId );
        }

        if ( MOVE_CATEGORY.equals( action ) ) 
        {
            int position = Integer.parseInt( req.getParameter( POSITION ) );

            manager.moveCategory( cat, position );

            out.println( "OK" );
        }
        else if ( DELETE_CATEGORY.equals( action ) )
        {
            manager.deleteCategory( cat );

            out.println( "OK" );
        }
        else if ( CREATE_CATEGORY.equals( action ) )
        {
            String name = req.getParameter( NAME );
            NewsCategory ncat = manager.newNewsCategory( name );

            out.println( ncat.getId() );
        }
        else if ( TOGGLE_STAR.equals( action ) )
        {
            String id = req.getParameter( NEWS_ITEM_ID );
            String sourceId = req.getParameter( NEWS_SOURCE_ID );
            boolean star = "true".equalsIgnoreCase( req.getParameter( STAR ) );
            boolean success = false;

            NewsSource source = manager.getNewsSourceForId( sourceId );
            if ( source != null )
            {
                success = source.setNewsItemStarred( id, star );
            }
            
            if ( success )
            {
                out.println( "OK" );
            }
            else
            {
                out.println( "ERROR" );
            }
        }

        out.close();
    }
}